﻿Imports System.Data.SqlClient
Partial Class upload_page
    Inherits System.Web.UI.Page

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        ' check file is there or not
        Try
            If FileUpload1.HasFile Then
                Dim content As String = FileUpload1.PostedFile.ContentType
                If content.Contains("image") Then ' to check its image or not
                    ' for generating filename to store at server
                    Dim filename As String = DateTime.Now.ToString.Replace(":", "")
                    filename = filename.Replace("/", "")
                    filename = filename.Replace(" ", "")
                    Dim extension As String = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."))

                    Dim finalname As String = filename + extension
                    Dim databasepath As String = "uploads/" + finalname
                    Dim storeat As String = Server.MapPath("uploads/") + finalname
                    FileUpload1.SaveAs(storeat)

                    Dim cn As New SqlConnection(ConfigurationSettings.AppSettings("conn"))
                    Dim sql As String = "insert into imagetable(categoryid,title,description,imageurl,uploaddate,uploadby,likes,dislikes) values(" + DropDownList1.SelectedValue + ",'" + TextBox1.Text + "','" + TextBox2.Text + "','" + databasepath + "','" + DateTime.Now + "','" + Session("username") + "',0,0)"
                    Dim cmd As New SqlCommand(sql, cn)
                    cn.Open()
                    If cmd.ExecuteNonQuery = 1 Then
                        Label1.Text = "Image Successfully uploaded"
                        TextBox1.Text = ""
                        TextBox2.Text = ""

                    End If
                    cn.Close()
                Else
                    Label1.Text = "You selected an Invalid Image"
                End If
            End If
        Catch ex As Exception
            ' Label1.Text = "Problem Occured!!!"
            Label1.Text = ex.Message

        End Try



          
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        Response.Redirect("default.aspx")

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session("username") = "Guest" Then
            Response.Redirect("login.aspx")
        End If
    End Sub
End Class
